<?php
session_start();
require_once('../config.php');
require('../smarty/smarty_config.php');

$id = $_GET['id'];
$f = $_GET['f'];

if (empty($_SESSION['USER_ID'])) {
	header('location:index.php');
	exit;
}
if (empty($f)){
	$f = "list";
}

$smarty -> display('./admin/header.tpl');
$smarty ->display('./admin/admin-left.tpl');

// setup a database connection
$db = new DB();
$db ->conn_mysql();

switch ($f) {
	// show the detail of the catalog with given indentity id
	case 'list':		
		$sql = "select * from catalog order by date_added desc";
		$catalog_rec = $db -> run_sql_query($sql);
		while ($catalog_info =  mysql_fetch_array($catalog_rec)) {
			$catalogs_list[] = $catalog_info;
		}
		echo count($catalogs_list);
		$smarty -> assign('f','show');
		$smarty -> assign('catalogs_list',$catalogs_list);
		$smarty -> display('./admin/catalog-list.tpl');
		
	case 'show':
		if (empty($id)) {
			header('location:index.php');
			exit;
		}		
		$sql = 'select * from catalog where id = '.$id;
		$catalog_rec = $db ->run_sql_query($sql);
		$catalog_info = mysql_fetch_array($catalog_rec);
		if ($catalog_info){
			$smarty -> assign('catalog',$catalog_info);
		}
		
		$smarty ->display('./admin/catalog.tpl');

	// add a catalog
	case 'add':		

		if (empty($_POST['OP']) || $_POST['OP'] ='' ) {
			$smarty	-> assign('f','add');
			$smarty -> assign('id',$id);
			$smarty -> display('./admin/add-catalog.tpl');
			$smarty -> display('./admin/foot.tpl');
		} else if($_POST['OP'] = 'submit') {				
			$sql = "insert into catalog values ('',now(),now(),'$_POST[catalog_name]','1')";
			
			$result = $db -> run_sql_query($sql);
			if ($result) {				
				$smarty -> display('./admin/ok.tpl');
			}
		}
		break;
	// update a catalog
	case 'modify':			
		if (empty($id)) {
			header('location:index.php');
			exit;
		}
		if (empty($_POST['OP'])){
			echo 'test';
			$sql = 'select * from catalog where id = '.$id;
			$catalog_rec = $db ->run_sql_query($sql);
			$catalog_info = mysql_fetch_array($catalog_rec);
			if ($catalog_info){
				$smarty	-> assign('f','modify');
				$smarty -> assign('id',$id);
				$smarty -> assign('catalog_info',$catalog_info);
				$smarty -> display('./admin/add-catalog.tpl');
			}
		}else if ($_catalog['OP'] ='submit') {
			echo $_POST[catalog_name];
			$sql = "update catalog set date_modified = now(),catalog_name= '$_POST[catalog_name]' where id = $id";
			
			$result = $db -> run_sql_query($sql);
			if ($result) {
				$smarty -> display('./admin/ok.tpl');
			}
		}
	// delete a catalog ,actully ,you know ,we always dont delete it ,just modify it.	
	case 'delete':
		if (empty($id)) {
			header('location:index.php');
			exit;
		}
		$sql = "update catalog set valid =0 ,date_modified = now() where id = $id";			
		$result = $db -> run_sql_query($sql);
		if ($result) {
			$smarty -> display('./admin/ok.tpl');
		}
		
	
	default:
		$smarty ->display('./admin/foot.tpl');
}



